package z_guestbook;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class GuestBookMain extends JFrame {
	Input input;
	Update update;
	Select select;
	
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";
	
	Connection conn;

	
	
	private JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnGuestBook;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GuestBookMain frame = new GuestBookMain();
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
					SwingUtilities.updateComponentTreeUI(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GuestBookMain() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 622, 418);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		try{
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url, db_user, db_pwd);
			
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("IP : " + ia.getHostAddress());
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		openSelect();
	}
	
	public void openInput(){
		if(input == null){
			input = new Input(this);
			contentPane.add(input);
		}
		input.setVisible(true);
		input.toFront();
		
	}
	public void openUpdate(){
		if(update == null){
			update = new Update(this);
			contentPane.add(update);
		}
		update.setVisible(true);
		update.toFront();
	}
	
	public void openSelect(){
		if(select == null){
			select = new Select(this);
			contentPane.add(select);
		}
		select.setVisible(true);
		select.toFront();
		
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnGuestBook());
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnGuestBook() {
		if (mnGuestBook == null) {
			mnGuestBook = new JMenu("Guest Book");
			mnGuestBook.add(getMntmNewMenuItem());
			mnGuestBook.add(getMntmNewMenuItem_1());
			mnGuestBook.add(getMntmNewMenuItem_2());
		}
		return mnGuestBook;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("입력");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openInput();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("수정 및 삭제");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openUpdate();				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("조회");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openSelect();				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("Connect");
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("서버연결");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String  ip = JOptionPane.showInputDialog(null, "접속할 서버 주소를 입력하세요");
					
					url = "jdbc:oracle:thin:@"+ ip + ":1521:xe";
					try{
						conn = DriverManager.getConnection(url, db_user, db_pwd);
						
						if(select != null) select.setData();
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
					
				}
			});
		}
		return mntmNewMenuItem_3;
	}
}
